package com.cysion.study.ui.fragment

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.widget.RelativeLayout
import android.widget.TextView
import com.cysion.other.startActivity_ex
import com.cysion.study.R
import com.cysion.study.adapter.HomeCourseAdapter
import com.cysion.study.adapter.HomeTeacherzuAdapter
import com.cysion.study.adapter.SourceAdapter
import com.cysion.study.entity.HomeCourses
import com.cysion.study.entity.Recruits
import com.cysion.study.entity.Researchstus
import com.cysion.study.helper.UserCache
import com.cysion.study.ktbox.Box
import com.cysion.study.ktbox.base.BaseFragment
import com.cysion.study.ktbox.base.ITEM_CLICK
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.study.net.ApiClient
import com.cysion.study.ui.NoticeActivity
import com.cysion.study.ui.personal.ActivityDetailActivity
import com.cysion.study.ui.personal.ClassDetailActivity
import com.cysion.study.ui.personal.ClassPushDetailActivity
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.widget.ShowToast
import com.cysion.study.widget.UIHelper
import com.cysion.targetfun._subscribe
import com.scwang.smartrefresh.layout.constant.RefreshState
import kotlinx.android.synthetic.main.fragment_pra_teacher1.*
import java.text.SimpleDateFormat
import java.util.*

/**
 * 私教新首页
 * Created by chu on 2019-06-30.
 */
class PrivateTeacherFragment1:BaseFragment(), View.OnClickListener {
    private lateinit var sourceAdapter: SourceAdapter
//    private val mSourceList: MutableList<CoursesEntity> = mutableListOf()
    override fun getLayoutId(): Int {
        return R.layout.fragment_pra_teacher1
    }

    override fun initView() {
        tv_notice.setOnClickListener(this)
        tv_attent.setOnClickListener(this)
        tv_appro.setOnClickListener(this)
        tv_source_more.setOnClickListener(this)
        tv_teac_more.setOnClickListener(this)

        initRecyclerView()
        initRefreshLayout()

        flow_course.removeAllViews()

        val simpleDateFormat = SimpleDateFormat("yyyy年MM月dd日")// HH:mm:ss
        //获取当前时间
        val date = Date(System.currentTimeMillis())
        tv_day.text=simpleDateFormat.format(date)


    }
    private lateinit var dataAdapter: HomeCourseAdapter
    private lateinit var teacherDataAdapter: HomeTeacherzuAdapter
    private val dataList: MutableList<Recruits> = mutableListOf()
    private val teacherDataList: MutableList<Recruits> = mutableListOf()

    private fun initRecyclerView() {
        //招聘
        rv_list.isNestedScrollingEnabled = false
        dataAdapter = HomeCourseAdapter(dataList, Box.context)
        rv_list.adapter = dataAdapter
        rv_list.layoutManager = LinearLayoutManager(Box.context)
        dataAdapter.setOnTypeClickListener { obj, position, flag ->
            if (flag == ITEM_CLICK) {//托管详情
                var bundle = Bundle()
                bundle.putString("type",obj.id)
                bundle.putString("name",obj.name)
                UIHelper.jump(activity, ClassPushDetailActivity::class.java,bundle)//推广课程详情
            }
        }
        //教师租赁
        rv_list_teacher.isNestedScrollingEnabled = false
        teacherDataAdapter = HomeTeacherzuAdapter(teacherDataList, Box.context)
        rv_list_teacher.adapter = teacherDataAdapter
        rv_list_teacher.layoutManager = LinearLayoutManager(Box.context)
        teacherDataAdapter.setOnTypeClickListener { obj, position, flag ->
            if (flag == ITEM_CLICK) {//托管详情
                var bundle = Bundle()
                bundle.putString("type",obj.id)
                bundle.putString("name",obj.name)
                UIHelper.jump(activity, ClassPushDetailActivity::class.java,bundle)//推广课程详情
            }
        }
    }

    //    初始化刷新控件
    private fun initRefreshLayout() {
        smartLayout.setEnableLoadMore(false)
        smartLayout.setOnRefreshListener {
            flow_course.removeAllViews()

            getDataList()
//            smartLayout.setEnableLoadMore(true)
        }
    }

    override fun initData() {
        super.initData()
        smartLayout.autoRefresh()
    }

    private fun getDataList() {
        Alert.loading(context)
        ApiClient.api.privateInfo(UserCache.token).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    stopRefresh()
                    //今日课程
                  /*  for (item in it.courses) {
                        println("###添加item数据类型"+item.type)
                        //type":1,// 类型：0.托管学校班级 1.课程班级 2、活动班级 3、研学班级
                        if (item.type == "0") {
                            addTgItem(item)//托管类型item
                        }else if (item.type == "1" || item.type == "3") {
                            addTodayCouse(item)//课程、研学item
                        }else if (item.type == "2") {
                            addActivity(item)//活动item
                        }
                    }*/
                    dataList.clear()
                    if (it.recruits.size == 0) {
                        multiView.showEmpty()
                        smartLayout.setEnableLoadMore(false)
                    } else {
                        dataList.addAll(it.recruits)
                        dataAdapter.notifyDataSetChanged()
                        multiView.showContent()
                    }

                    if (it.notReadNotice == "0") {
                        tv_num.visibility = View.GONE
                    } else {
                        tv_num.visibility = View.VISIBLE
                        tv_num.text = it.notReadNotice
                    }

                }
                _onError {
                    Alert.close()
                    stopRefresh()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                }
            }
    }

    private fun addTgItem(item: HomeCourses) {
        println("###添加item托管")
        val v = LayoutInflater.from(context).inflate(R.layout.flow_home_tg_item, flow_course, false)
        var tv_course_title = v.findViewById<TextView>(R.id.tv_course_title)
        var tv_time = v.findViewById<TextView>(R.id.tv_time)
        var rl_course = v.findViewById<RelativeLayout>(R.id.rl_course)
        tv_course_title.text=item.name
        tv_time.text=item.startTime+"---"+item.endTime
        rl_course.setOnClickListener {
            ShowToast("托管详情")
        }
        flow_course.addView(v)

    }

    private fun addActivity(item: HomeCourses) {
        println("###添加item活动")
        val v = LayoutInflater.from(context).inflate(R.layout.flow_home_activity, flow_course, false)
        var tv_activity_title = v.findViewById<TextView>(R.id.tv_activity_title)
        var tv_time = v.findViewById<TextView>(R.id.tv_time)
        var tv_address = v.findViewById<TextView>(R.id.tv_address)
        var rl_activity = v.findViewById<RelativeLayout>(R.id.rl_activity)
        tv_activity_title.text=item.name
        tv_time.text=item.startTime+"---"+item.endTime
        tv_address.text=item.address
        rl_activity.setOnClickListener {//活动详情
            var bundle = Bundle()
            bundle.putString("id",item.id)//日程id
            bundle.putString("cid",item.cid)//班级id
            bundle.putString("activityName",item.name)//
            UIHelper.jump(activity, ActivityDetailActivity::class.java,bundle)
        }
        flow_course.addView(v)
    }

    //课程
    private fun addTodayCouse(item: HomeCourses) {
        println("###添加item课程")
        val v = LayoutInflater.from(context).inflate(R.layout.flow_home_couse, flow_course, false)
        var tv_course_title = v.findViewById<TextView>(R.id.tv_course_title)
        var tv_time = v.findViewById<TextView>(R.id.tv_time)
        var tv_num = v.findViewById<TextView>(R.id.tv_num)
        var rl_course = v.findViewById<RelativeLayout>(R.id.rl_course)
        tv_course_title.text=item.name
        tv_time.text=item.startTime+"---"+item.endTime
        tv_num.text = item.currentNumber + "/" + item.totalNumber
        rl_course.setOnClickListener {//课程详情
            var bundle = Bundle()
            bundle.putString("courseId",item.id)//课时id
            UIHelper.jump(activity, ClassDetailActivity::class.java,bundle)//课程详情
        }
        flow_course.addView(v)
    }


    private fun stopRefresh() {
        if (smartLayout.state == RefreshState.Refreshing) {
            smartLayout.finishRefresh()
        } else if (smartLayout.state == RefreshState.Loading) {
            smartLayout.finishLoadMore(100)
        }
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_notice -> {//通知列表
                tv_num.visibility = View.GONE
                context.startActivity_ex<NoticeActivity>()
            }
            R.id.tv_attent ->{//教师租赁
//                context.startActivity_ex<AttendenceActivity>()
            }
            R.id.tv_appro ->{//招聘
//                context.startActivity_ex<MyApprovalActivity>()

            }
            R.id.tv_source_more ->{//查看所有日程
//                context.startActivity_ex<CourseMoreActivity>()旧课表

            }
            R.id.tv_teac_more ->{
//                ShowToast("活动更多")
//                context.startActivity_ex<ActivityManageActivity>()
            }

            /*R.id.tv_today_attent ->{//今日考勤
                context.startActivity_ex<AttentActivity>()
            }
            R.id.tv_comment ->{//托管点评
                context.startActivity_ex<AttentCommentActivity>()
            }*/
        }
    }

   /* override fun loading() {
        Alert.loading(context)
    }

    override fun stopLoad() {
        if (smartLayout.state == RefreshState.Refreshing) {
            smartLayout.finishRefresh()
        } else if (smartLayout.state == RefreshState.Loading) {
            smartLayout.finishLoadMore(100)
        }
        Alert.close()
    }

    override fun error(code: Int, msg: String) {
        smartLayout.finishRefresh()
        ShowToast(msg)
    }*/
    override fun closeMvp() {
    }
}